package com.sec.android.app.myfiles.feature.Hover;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.recent.RecentAdapterImp;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.util.UiUtils;

public class RecentHoverView extends FolderHoverView {

    public RecentHoverView(Context context, FileRecord record, AbsMyFilesFragment fragment) {
        super(context, record, fragment);
    }

    @Override
    protected Cursor getHoverCursor() {
        int mMaxItemCount = UiUtils.getHomeRecentMaxItemCnt(mFragment.getActivity(), mFragment.getProcessId());
        Uri recentUri = Uri.parse(DbTableInfo.getInstance(FileRecord.StorageType.Recent).getUri());
        Cursor ret = null;
        try (Cursor cursor = mContext.getContentResolver().query(recentUri, null, null, null, null)) {
            if (cursor != null) {
                int moreCount = cursor.getCount() - mMaxItemCount + 1;
                recentUri = recentUri.buildUpon().encodedQuery("limit=" + (mMaxItemCount - 1) + "," + moreCount).build();
                ret = mContext.getContentResolver().query(recentUri, null, null, null, "recent_date DESC");
            }
        }
        return ret;

    }

    @Override
    protected AbsFileListAdapterImp getHoverAdapter(NavigationInfo navigationInfo, Cursor ret) {
        return new RecentAdapterImp(mContext, navigationInfo, R.layout.airview_preview_list_item_grid_common, ret, 0);
    }
}
